// functionPage/pages/userPassword/userPassword.js
import throttle from '../../../utils/throttle.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 口令弹框
        show: false,
        // 口令
        password: '',
        // 口令类型
        passwordType: '00',
        // 会议类型口令数目
        meetingpwd: 0,
        // 单人类型口令数目
        singlepwd: 0,
        loading: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getValidPasswordCount()
    },

    // 创建口令，type '00'单人 '01'会议
    createVisitorPassword: throttle(function(e) {
        let _that = this
        let {
            type
        } = e.currentTarget.dataset

        // 先立即打开弹窗，防止请求慢导致用户多次点击
        // 口令要清除之前的
        _that.setData({
            loading: true,
            show: true,
            password: '',
            passwordType: type
        })

        wx.request({
            url: app.globalData.rootPath + `cjMap/createVisitorPassword`,
            data: {
                authId: app.globalData.authId,
                type
            },
            success: function(res) {
                if (res.data.status === "0") {
                    _that.setData({
                        loading: false,
                        password: res.data.data.password
                    })
                    // 重新请求数字
                    _that.getValidPasswordCount()
                }
            }
        });
    }, 200, {
        'trailing': false
    }),

    // 复制
    copyText: function(e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.content,
            success: function(res) {
                wx.getClipboardData({
                    success: function(res) {
                        wx.showToast({
                            title: '口令复制成功',
                            icon: 'none',
                            duration: 1500
                        })
                    }
                })
            }
        })
    },

    // 关闭弹窗
    close() {
        this.setData({
            show: false
        })
    },

    // 获取口令有效数字
    getValidPasswordCount() {
        let _that = this
        wx.request({
            url: app.globalData.rootPath + `cjMap/countPasswordType`,
            data: {
                authId: app.globalData.authId
            },
            success: function(res) {
                if (res.data.status === "0") {
                    _that.setData({
                        meetingpwd: res.data.data.meetingpwd,
                        singlepwd: res.data.data.singlepwd
                    })
                }
            }
        });
    },

    changeNav: throttle(e => {
        let {
            nav
        } = e.currentTarget.dataset;
        console.log(nav);
        wx.navigateTo({
            url: `${nav}`
        })
    }, 200, {
        'trailing': false
    }),
})